package org.uim.cricket.analyzer.parsers.batting.impl;

import java.util.HashMap;

import org.uim.cricket.analyzer.parsers.batting.BattingInningParsable;
import org.uim.cricket.analyzer.parsers.batting.OutDetailsParsable;
import org.uim.cricket.analyzer.parsers.misc.URLIDParsable;
import org.uim.cricket.analyzer.parsers.misc.URLTitleParsable;
import org.uim.cricket.entities.Player;
import org.uim.cricket.entities.batting.BatsmanOutDetails;
import org.uim.cricket.entities.batting.BatsmanStatistics;
import org.uim.cricket.entities.batting.BattingInning;
import org.uim.cricket.util.HTMLStringUtils;
import org.uim.cricket.util.MyStringUtils;

public class DefaultBattingInningParser implements BattingInningParsable {

	private OutDetailsParsable outDetailParser = null;
	private URLIDParsable urlIdParser = null;
	private URLTitleParsable urlTitleParser = null;
	
	public DefaultBattingInningParser(OutDetailsParsable theOutDetailParser) {
		this.outDetailParser = theOutDetailParser;
	}
	
	public DefaultBattingInningParser(OutDetailsParsable theOutDetailParser,
	        URLIDParsable theUrlIDParser, URLTitleParsable theUrlTitleParser) {
        this.outDetailParser = theOutDetailParser;
        this.urlIdParser = theUrlIDParser;
        this.urlTitleParser = theUrlTitleParser;
    }
	
	@Override
	public BattingInning parseBattingInning(StringBuilder sb, HashMap<String, Integer> header) {
	    if (sb == null || sb.length() == 0) return null;
	    BattingInning bat = new BattingInning();
	    
	    String source = sb.toString();
	    String[] rows = MyStringUtils.splitInBetween(source, "<td", "</td>");

	    if (rows.length < (3 + header.size())) return null;
	    
	    this.setPlayerNameRow(rows[1], bat);
	    
	    String outdet = HTMLStringUtils.cleanHTMLTags("<" + rows[2]).trim();
	    BatsmanOutDetails outd = outDetailParser.parseOutDetails(outdet);
	    bat.setOutDetails(outd); 
	    
	    BatsmanStatistics stats = new BatsmanStatistics();
	    for (String key : header.keySet()) {
	        int index = header.get(key);
	        
	        if (key.startsWith("R")) {
	            String stat = HTMLStringUtils.cleanHTMLTags("<" + rows[index]).trim();
	            if (MyStringUtils.isNumeric(stat)) {
	                stats.setRuns(Integer.parseInt(stat));
	            }
	        } else if (key.startsWith("M")) {
	            String stat = HTMLStringUtils.cleanHTMLTags("<" + rows[index]).trim();
                if (MyStringUtils.isNumeric(stat)) {
                    stats.setMins(Integer.parseInt(stat));
                }
	        } else if (key.startsWith("B")) {
	            String stat = HTMLStringUtils.cleanHTMLTags("<" + rows[index]).trim();
                if (MyStringUtils.isNumeric(stat)) {
                    stats.setBalls(Integer.parseInt(stat));
                }
            } else if (key.startsWith("4")) {
                String stat = HTMLStringUtils.cleanHTMLTags("<" + rows[index]).trim();
                if (MyStringUtils.isNumeric(stat)) {
                    stats.setFours(Integer.parseInt(stat));
                }
            } else if (key.startsWith("6")) {
                String stat = HTMLStringUtils.cleanHTMLTags("<" + rows[index]).trim();
                if (MyStringUtils.isNumeric(stat)) {
                    stats.setSixes(Integer.parseInt(stat));
                }
            }
	    }
	    bat.setStatistics(stats);

		return bat;
	}
	
	private void setPlayerNameRow(String row, BattingInning bat) {
	    int pos = row.indexOf('>');
	    row = row.substring(pos + 1);
	    
	    pos = row.indexOf('<');
	    int epos = row.indexOf('>', pos + 1);
	    String playerURLTag = row.substring(pos, epos + 1);
	    
	    HashMap<String, String> attribs = HTMLStringUtils.getHTMLAttributes(playerURLTag);
	    Player player = new Player();
	    if (this.urlIdParser != null) {
	        player.setId(this.urlIdParser.parseUrlID(attribs.get("href")));
	    } else {
	        player.setId(attribs.get("href"));
	    }
    	player.setPlayerLink(attribs.get("href"));
    	
	    if (this.urlTitleParser != null) {
	        player.setShortName(this.urlTitleParser.parseUrlTitle(attribs.get("title")));
	    } else {
	        player.setShortName(attribs.get("title"));
	    }
	    
	    
	    String cleanedName = HTMLStringUtils.cleanHTMLTags(row);
	    if (cleanedName.indexOf('*') >= 0) {
	        player.setCaptain(true);
	        cleanedName = cleanedName.replace("*", "");
	    }
	    if (cleanedName.indexOf("&dagger;") >= 0) {
            player.setWicketKeeper(true);
            cleanedName = cleanedName.replace("&dagger;", "");
        }
	    cleanedName = cleanedName.trim();
	    player.setTitleName(cleanedName);
	    
	    bat.setBatsman(player);
	}
	
	public void setOutDetailParser(OutDetailsParsable outDetailParser) {
		this.outDetailParser = outDetailParser;
	}
	
	public OutDetailsParsable getOutDetailParser() {
		return outDetailParser;
	}
	
	public void setUrlTitleParser(URLTitleParsable urlTitleParser) {
        this.urlTitleParser = urlTitleParser;
    }
	
	public void setUrlIdParser(URLIDParsable urlIdParser) {
        this.urlIdParser = urlIdParser;
    }
	
	public URLTitleParsable getUrlTitleParser() {
        return urlTitleParser;
    }
	
	public URLIDParsable getUrlIdParser() {
        return urlIdParser;
    }
	
}
